import yhTools from "./yh";
import store from "../Redux/Store/Store";
import { getAppPlatformsDatas } from "../Redux/Action/nativeDatas/nativeDatas";
import { channelCode, version } from "../config/Config";
import { Toast } from "antd-mobile";

export function platform() {
	/* if (process.env.NODE_ENV == "development") {
		return "web";
	} */
	let platform = "",
		u = navigator.userAgent;
	if (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1) {
		platform = "android";
	} else if (u.indexOf("iPhone") > -1 || u.indexOf("iPad") > -1) {
		platform = "ios";
	} else {
		platform = "web";
	}
	return platform;
}

//弹窗: 1段内容+1个按钮
export function alertOneButt(title, butt1) {
	if (document.getElementById("alert") == undefined) {
		let alertId = "alert";
		let btnId = "closeBtn";
		let alertSigleDom = document.createElement("div");
		alertSigleDom.setAttribute("id", alertId);
		document.body.appendChild(alertSigleDom);
		alertSigleDom.innerHTML = `<div class='dialog-alert'>
                <div class='dialog-con'>
                    <span class='dialog-body'>${title}</span>
                    <div class='dialog-foot'>
                        <button class='btn-alert-ok' id=${btnId}>${butt1}</button>
                    </div>
                </div>
            </div>`;
		document.getElementById("closeBtn").onclick = () => {
			document.body.removeChild(document.getElementById("alert"));
		};
	}
}

// 弹窗: 1段内容 + 2个按钮
export function alertTwoButt(msg, butt1, butt2, conformBack) {
	if (document.getElementById("confirm") == undefined) {
		let confirmId = "confirm";
		let okBtnId = "okBtn";
		let cancelBtnId = "cancelBtn";
		let confirmSigleDom = document.createElement("div");
		confirmSigleDom.setAttribute("id", confirmId);
		document.body.appendChild(confirmSigleDom);
		confirmSigleDom.innerHTML = `<div class='dialog-alert'>
                <div class='dialog-con'>
                    <span class='dialog-body'>${msg}</span>
                    <div class='dialog-foot'>
                        <a class='btn-cancel btn-confrim' id=${cancelBtnId}>${butt2}</a>
                        <a class='btn-ok btn-confrim' id=${okBtnId}>${butt1}</a>
                    </div>
                </div>
            </div>`;
		document.getElementById("cancelBtn").onclick = () => {
			document.body.removeChild(document.getElementById("confirm"));
		};
		document.getElementById("okBtn").onclick = () => {
			document.body.removeChild(document.getElementById("confirm"));
			conformBack();
		};
	}
}

/**
 * 打开设置界面
 */
export function settingPrivacy() {
	new Promise(function (resolve) {
		window.YHAuthorize.callHandler("Device_Setting", "", resp => {
			resolve(resp);
		});
	});
}

/**
 * toast
 */
export function toast(msg, sec) {
	Toast.info(msg, sec);
}

/**
 * TalkingData 统计
 */
export function TDAPPonEvent(key) {
	if (process.env.NODE_ENV == "development") {
		return;
	}
	if (!key || key == "") {
		return;
	}
	let jsonData = {
		eventID: key,
		label: "",
		parameters: {}
	};
	window.YHAuthorize.callHandler("TalkingData_Event", jsonData, () => {});
}

/**
 * TalkingData统计和获取设备信息，地理位置存入后台
 */
export function getStatisticsDatas(pageIndex, props) {
	let deviceInfo = props.getDeviceInfoDatas.deviceInfo; //设备信息
	let postAjax = props._postDatas;
	let authHeader = props.getAppPlatformsDatas; //头部信息

	let bodyParams = {
		optType: pageIndex, //必传参数,参见下面说明信息
		longitude: "", //选填参数
		latitude: "", //选填参数
		deviceId: deviceInfo.uuid, //选填参数
		deviceInfo: deviceInfo.phone_platform, //选填参数
		deviceVersion: deviceInfo.system_version, //选填参数
		plateform: deviceInfo.system_name, //选填参数
		wifiMac: deviceInfo.wifi_mac, //选填参数
		installedList: deviceInfo.installedList, //选填参数
		batteryCapacity: deviceInfo.battery_level,
		aid: deviceInfo.idfa,
		appBundleId: deviceInfo.app_bundle_id,
		appVersion: deviceInfo.app_version,
		sdkVersion: deviceInfo.sdk_version,
		systemVersion: deviceInfo.system_version,
		sourceCode: "", //选填参数
		activityCode: "", //选填参数
		clientId: "", //选填参数
		gyroscopex: "", //选填参数
		gyroscopey: "", //选填参数
		gyroscopez: "", //选填参数
		imei: "", //选填参数
		imsi: ""
	};

	let path = "/user/uploadDeviceInfo/0";

	//上传数据
	if (process.env.NODE_ENV == "development") {
		postAjax(
			path,
			authHeader,
			bodyParams,
			() => {},
			"uploadDeviceInfo",
			postAjax
		);
	} else {
		//统计
		yhTools.TDAPPonEvent(pageIndex);
		//获取经纬度
		window.YHAuthorize.callHandler("Privacy_Loaction", "", res => {
			if (res.status == "1000") {
				bodyParams.longitude = res.data.longitude;
				bodyParams.latitude = res.data.latitude;
			}
			postAjax(
				path,
				authHeader,
				bodyParams,
				() => {},
				"uploadDeviceInfo",
				postAjax
			);
		});
	}
}

/**
 * 获取设备信息，地理位置，陀螺仪信息
 */
export function getNativeInfo(resolve, bodyParams) {
	if (process.env.NODE_ENV != "development") {
		//开发环境
		window.YHAuthorize.callHandler("Device_Environment", "", res => {
			//获取设备信息
			bodyParams.deviceInfo = {
				clientId: res.data.jpush_id, //Jpush唯一标识
				deviceInfo: res.data.phone_platform, // 设备型号
				deviceVersion: res.data.system_version, //系统版本
				plateform: res.data.system_name, //Android，iOS
				appVersion: version.versionName, //App版本号
				uuid: res.data.system_name == "iOS" ? res.data.idfa : res.data.uuid, //UUID
				batteryCapacity: res.data.battery_level, //电池电量
				imsi: "", //imsi
				wifiMac: res.data.wifi_mac, //wifiMac
				installedList: res.data.installedList, //安装应用列表(数组类型)
				smId: res.data.sm_device_id //数美设备ID
			};

			store.dispatch(
				getAppPlatformsDatas({
					openId: "",
					unionid: "",
					channelCode: channelCode ? channelCode : "01", //渠道号
					appVersion: version.versionName, //App版本号
					deviceInfo: res.data.phone_platform, // 设备型号
					deviceVersion: res.data.system_version, //系统版本
					plateform: res.data.system_name, //Android，iOS
					deviceId:
						res.data.system_name == "iOS" ? res.data.idfa : res.data.uuid, //UUID
					macAddress: res.data.wifi_mac, //wifiMac
					batteryCapacity: res.data.battery_level, //电池电量
					clientId: res.data.jpush_id, //
					smId: res.data.sm_device_id //数美设备ID
				})
			);

			window.YHAuthorize.callHandler("Privacy_Loaction", "", res => {
				//获取地理位置信息
				bodyParams.locationInfo = {
					latitude: res.data.latitude, //纬度
					longitude: res.data.longitude //经度
				};
				store.dispatch(
					getAppPlatformsDatas({
						latitude: res.data.latitude, //纬度
						longitude: res.data.longitude //经度
					})
				);
				setTimeout(() => {
					resolve();
				}, 1000);
			});
		});
	} else {
		store.dispatch(
			getAppPlatformsDatas({
				openId: "",
				unionid: "",
				channelCode: "01", //渠道号
				appVersion: "2.2.0", //App版本号
				deviceInfo: "1111", // 设备型号
				deviceVersion: "10.0", //系统版本
				plateform: "Android", //Android，iOS
				deviceId: "", //设备唯一标识 android：imei，IOS_IDFA：idfa
				macAddress: "", //mac地址
				batteryCapacity: "", //电池电量
				clientId: "", //
				smId: "" //数美设备ID
			})
		);
		resolve();
	}
}

export function environmentInfo() {
	if (process.env.NODE_ENV != "development") {
		//开发环境
		return new Promise(resolve => {
			window.YHAuthorize.callHandler("Device_Environment", "", res => {
				//获取设备信息
				store.dispatch(
					getAppPlatformsDatas({
						openId: "",
						unionid: "",
						channelCode: channelCode ? channelCode : "01", //渠道号
						appVersion: version.versionName, //App版本号
						deviceInfo: res.data.phone_platform, // 设备型号
						deviceVersion: res.data.system_version, //系统版本
						plateform: res.data.system_name, //Android，iOS
						deviceId:
							res.data.system_name == "iOS" ? res.data.idfa : res.data.uuid, //UUID
						macAddress: res.data.wifi_mac, //wifiMac
						batteryCapacity: res.data.battery_level, //电池电量
						clientId: res.data.jpush_id, //
						smId: res.data.sm_device_id //数美设备ID
					})
				);
				resolve(res.data);
			});
		});
	} else {
		store.dispatch(
			getAppPlatformsDatas({
				openId: "",
				unionid: "",
				channelCode: "01", //渠道号
				appVersion: "2.2.0", //App版本号
				deviceInfo: "1111", // 设备型号
				deviceVersion: "10.0", //系统版本
				plateform: "Android", //Android，iOS
				deviceId: "", //设备唯一标识 android：imei，IOS_IDFA：idfa
				macAddress: "", //mac地址
				batteryCapacity: "", //电池电量
				clientId: "", //
				smId: "" //数美设备ID
			})
		);
	}
}

export function loactionInfo() {
	if (process.env.NODE_ENV != "development") {
		//开发环境
		return new Promise(resolve => {
			window.YHAuthorize.callHandler("Privacy_Loaction", "", res => {
				//获取地理位置信息
				store.dispatch(
					getAppPlatformsDatas({
						latitude: res.data.latitude, //纬度
						longitude: res.data.longitude //经度
					})
				);
				resolve(res.data);
			});
		});
	} else {
		store.dispatch(
			getAppPlatformsDatas({
				longitude: 0,
				latitude: 0
			})
		);
		return {
			longitude: 0,
			latitude: 0
		};
	}
}
